package com.lzq.mq.broker.core;

import com.lzq.mq.broker.cache.CommonCache;
import com.lzq.mq.broker.model.ConsumeQueueModel;
import com.lzq.mq.broker.model.MqTopicModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 消费队列追加处理器
 * - 初始化导入消费队列MMap映射
 */
public class ConsumeQueueAppendHandler {

    /**
     * 准备消费队列MMap映射
     */
    public void prepareConsumeQueue(String topicName) {

        Map<String, MqTopicModel> mqTopicModelMap = CommonCache.getMqTopicModelMap();
        MqTopicModel mqTopicModel = mqTopicModelMap.get(topicName);
        // 获取消费队列列表
        List<ConsumeQueueModel> queueList = mqTopicModel.getQueueList();
        // 映射MMap
        List<ConsumeQueueMMapFileModel> consumeQueueMMapFileModelList = new ArrayList<>();
        for (ConsumeQueueModel consumeQueueModel : queueList) {
            ConsumeQueueMMapFileModel consumeQueueMMapFileModel = new ConsumeQueueMMapFileModel();
            consumeQueueMMapFileModel.loadFileInMMap(topicName, consumeQueueModel);
            consumeQueueMMapFileModelList.add(consumeQueueMMapFileModel);
        }
        CommonCache.getConsumeQueueMMapFileModelManager().put(topicName, consumeQueueMMapFileModelList);
    }
}
